EXEC := lerp
DIRSRC := src
DIROBJ := obj
DIRINC := include
DIRLIB := lib

ifeq ($(library), dynamic)
	LIB := libPoint.so
	CLIB := ar rs
else
	library := static
	LIB := libPoint.a
	CLIB := gcc -shared -fPIC -o
endif

CXX := g++

# Flags de compilación -----------------------------------------------
CXXFLAGS := -I$(DIRINC) -Wall -fPIC

# Flags del linker ---------------------------------------------------
LDFLAGS := -L$(DIRLIB) -lPoint

.PHONY: all dirs clean

all: dirs info $(EXEC)

dirs:
	if [ ! -d $(DIROBJ) ]; then mkdir $(DIROBJ); fi;
	if [ ! -d $(DIRLIB) ]; then mkdir $(DIRLIB); fi;

info:
	@echo '------------------------------------'
	@echo 'make [library=static|dynamic]   '
	@echo '------------------------------------'
	@echo 'Elegido $(library)'
ifeq ($(library), dynamic)
	@echo 'No olvide actualizar LD_LIBRARY_PATH'
endif
	@echo '------------------------------------'

# Enlazado -----------------------------------------------------------
$(EXEC): $(DIROBJ)/main.o $(DIRLIB)/$(LIB)
	$(CXX) $(LDFLAGS) -o $@ $^

# Compilación --------------------------------------------------------
$(DIROBJ)/Point.o: $(DIRSRC)/Point.cpp $(DIRINC)/Point.h
	$(CXX) $(CXXFLAGS) -c $< -o $@

$(DIROBJ)/main.o: $(DIRSRC)/main.cpp $(DIRINC)/Point.h
	$(CXX) $(CXXFLAGS) -c $< -o $@

$(DIRLIB)/$(LIB): $(DIROBJ)/Point.o
	$(CLIB) $@ $<

# Limpieza -----------------------------------------------------------
clean:
	$(RM) -rf $(DIROBJ) $(DIRLIB)
	$(RM) -f *.log $(EXEC) *~ $(DIRSRC)/*~
